package com.summer.data.jql.builder;

import com.summer.data.Jsql;
import com.summer.data.dialect.Dialect;
import com.summer.data.jql.Context;
import com.summer.data.jql.JsqlAble;
import com.summer.data.jql.behavior.OrderAble;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 345480567@qq.com
 */

public class OrderBuilder implements JsqlAble {

    List<OrderAble> orderElemList = new ArrayList<>();

    Context ctx;

    public OrderBuilder(Context ctx) {
        this.ctx = ctx;
    }

    public void order(OrderAble... exps) {
        for (OrderAble orderAble : exps) {
            orderElemList.add(orderAble);
        }
    }

    @Override
    public Jsql buildJsql(Dialect dialect) {
        Jsql jsql = new Jsql();

        for (int i=0; i<orderElemList.size(); i++) {
            OrderAble orderElem = orderElemList.get(i);
            if (i==0) {
                jsql.append(" ORDER BY ");
            } else {
                jsql.append(", ");
            }

            Jsql expJsql = orderElem.toOrderSql(ctx, dialect);
            jsql.append(expJsql);
        }
        return jsql;
    }

}

